﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMP.ERP.Model;
using CMP.ERP.BLL;
using System.Data;

namespace CMP.ERP.Admin.Models
{
    public class ClientController : Controller
    {
        //
        // GET: /Client/
        //实例化ClientMessageBLL对象 
        ClientMessageBLL bll = new ClientMessageBLL();
        //实例化ClientMessageBLL对象
        ProvinceBLL pbll = new ProvinceBLL();

        /// <summary>
        /// 显示客户管理视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Client_Show()
        {
            //获取省份下拉框信息
            List<Province> plist = new List<Province>();
            DataTable dat = pbll.GetProvince();
            foreach (DataRow item in dat.Rows)
            {
                Province p = new Province();
                p.P_Id = Convert.ToInt32(item[0]);
                p.P_Name = item[1].ToString();
                p.P_City = item[2].ToString();
                plist.Add(p);
            }
            ViewBag.P_Id = plist;
            //获取客户管理信息
            List<ClientMessage> list = new List<ClientMessage>();
            DataTable data = bll.ClientMessageAll();
            foreach (DataRow item in data.Rows)
            {
                ClientMessage c = new ClientMessage();
                c.Client_Id = Convert.ToInt32(item[0]);
                c.Client_Name = item[1].ToString();
                c.Client_Phone = item[2].ToString();
                c.Client_Address = item[3].ToString();
                c.Client_Email = item[4].ToString();
                c.Client_Remark = item[5].ToString();
                c.P_Id = Convert.ToInt32(item[6]);
                list.Add(c);
            }
            return View(list);
        }
        /// <summary>
        /// 实现删除功能  
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Custom_Del(int id)
        {
            if (bll.ClientMessageDelete(id) > 0)
            {
                return Content("<script>alert('删除成功！'); location.href='/Client/Client_Show'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败！'); location.href='/Client/Client_Show'</script>");
            }
        }

        //批量删除客户管理信息
        public ActionResult DeleteAll(string Client_Id)
        {
            bool isdelete = bll.ClientMessageDeleteAll(Client_Id);

            return Content(isdelete.ToString());
        }

        /// <summary>
        /// 实现添加功能
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public ActionResult Custom_Info_Add(ClientMessage c)
        {
            if (bll.AddClientMessage(c) > 0)
            {
                return Content("<script>alert('添加成功！'); location.href='/Client/Client_Show'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败！'); location.href='/Client/Client_Show'</script>");
            }
        }
        /// <summary>
        /// 弹出修改视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Custom_Info_Upt(int id)
        {
            List<Province> plist = new List<Province>();
            DataTable dat = pbll.GetProvince();
            foreach (DataRow item in dat.Rows)
            {
                Province p = new Province();
                p.P_Id = Convert.ToInt32(item[0]);
                p.P_Name = item[1].ToString();
                p.P_City = item[2].ToString();
                plist.Add(p);
            }
            ViewBag.P_Id = plist;
            DataTable dt = bll.GetClientMessage(id);
            ClientMessage p2 = new ClientMessage();
            foreach (DataRow  item in dt.Rows)
            {
                p2.Client_Id = Convert.ToInt32(item["Client_Id"]);
                p2.Client_Name = item["Client_Name"].ToString();
                p2.Client_Phone = item["Client_Phone"].ToString();
                p2.Client_Remark = item["Client_Remark"].ToString();
                p2.Client_Address = item["Client_Address"].ToString();
                p2.Client_Email = item["Client_Email"].ToString();
                p2.P_Id = Convert.ToInt32(item["P_Id"]);
              
            }
            return View(p2);
        }
        /// <summary>
        /// 实现修改功能
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public ActionResult Custom_Info_UptDo(ClientMessage client)
        {
            if (bll.UptClientMessage(client) > 0)
            {
                return Content("<script>alert('修改成功！'); location.href='/Client/Client_Show'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败！'); location.href='/Client/Client_Show'</script>");
            }
        }
        /// <summary>
        /// 实现搜索功能
        /// </summary>
        /// <param name="Client_Name"></param>
        /// <returns></returns>
        public ActionResult Serach_Custom_Info(string Client_Name)
        {
            //获取省份下拉框信息
            List<Province> plist = new List<Province>();
            DataTable dat = pbll.GetProvince();
            foreach (DataRow item in dat.Rows)
            {
                Province p = new Province();
                p.P_Id = Convert.ToInt32(item[0]);
                p.P_Name = item[1].ToString();
                p.P_City = item[2].ToString();
                plist.Add(p);
            }
            ViewBag.P_Id = plist;
            //获取客户管理信息
            List<ClientMessage> list = new List<ClientMessage>();
            DataTable data = bll.SearchClientMessage(Client_Name);
            foreach (DataRow item in data.Rows)
            {
                ClientMessage c = new ClientMessage();
                c.Client_Id = Convert.ToInt32(item[0]);
                c.Client_Name = item[1].ToString();
                c.Client_Phone = item[2].ToString();
                c.Client_Address = item[3].ToString();
                c.Client_Email = item[4].ToString();
                c.Client_Remark = item[5].ToString();
                c.P_Id = Convert.ToInt32(item[6]);
                list.Add(c);
            }
            
            return View("Client_Show",list);        
        }
        /// <summary>
        /// 获取下拉框
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCitySelect()
        {
            List<Province> plist = new List<Province>();
            DataTable dat = pbll.GetProvince();
            foreach (DataRow item in dat.Rows)
            {
                Province p = new Province();
                p.P_Id = Convert.ToInt32(item[0]);
                p.P_Name = item[1].ToString();
                p.P_City = item[2].ToString();
                plist.Add(p);
            }
            return Json(plist, JsonRequestBehavior.AllowGet);
        }
    }
}
